package cn.iocoder.an.module.business.controller.admin.auditarea;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.auditarea.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditarea.AuditAreaDO;
import cn.iocoder.an.module.business.service.auditarea.AuditAreaService;

@Tag(name = "管理后台 - 分层审核区域设置")
@RestController
@RequestMapping("/business/audit-area")
@Validated
public class AuditAreaController {

    @Resource
    private AuditAreaService auditAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建分层审核区域设置")
    @PreAuthorize("@ss.hasPermission('business:audit-area:create')")
    public CommonResult<Long> createAuditArea(@Valid @RequestBody AuditAreaSaveReqVO createReqVO) {
        return success(auditAreaService.createAuditArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分层审核区域设置")
    @PreAuthorize("@ss.hasPermission('business:audit-area:update')")
    public CommonResult<Boolean> updateAuditArea(@Valid @RequestBody AuditAreaSaveReqVO updateReqVO) {
        auditAreaService.updateAuditArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分层审核区域设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:audit-area:delete')")
    public CommonResult<Boolean> deleteAuditArea(@RequestParam("id") Long id) {
        auditAreaService.deleteAuditArea(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除分层审核区域设置")
    @PreAuthorize("@ss.hasPermission('business:audit-area:delete')")
    public CommonResult<Boolean> deleteAuditAreaList(@RequestParam("ids") List<Long> ids) {
        auditAreaService.deleteAuditAreaListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得分层审核区域设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:audit-area:query')")
    public CommonResult<AuditAreaRespVO> getAuditArea(@RequestParam("id") Long id) {
        AuditAreaDO auditArea = auditAreaService.getAuditArea(id);
        return success(BeanUtils.toBean(auditArea, AuditAreaRespVO.class));
    }


    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得分层审核区域设置分页")
    @PreAuthorize("@ss.hasPermission('business:audit-area:query')")
    public CommonResult<PageResult<AuditAreaRespVO>> getAuditAreaPage(@Valid AuditAreaPageReqVO pageReqVO) {
        PageResult<AuditAreaDO> pageResult = auditAreaService.getAuditAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditAreaRespVO.class));
    }

    @GetMapping("/getIds")
    @Operation(summary = "获得分层审核区域已绑定的部门ID")
    public CommonResult<List<Long>> getAuditAreaDeptIds() {
        return success(auditAreaService.getAuditAreaDeptIds());
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出分层审核区域设置 Excel")
    @PreAuthorize("@ss.hasPermission('business:audit-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditAreaExcel(@Valid AuditAreaPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditAreaDO> list = auditAreaService.getAuditAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分层审核区域设置.xls", "数据", AuditAreaRespVO.class,
                BeanUtils.toBean(list, AuditAreaRespVO.class));
    }

}